package com.zg.invoice.web;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.zg.common.core.R;
import com.zg.invoice.service.InvoiceService;
import com.zg.invoice.vo.*;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.v3.oas.annotations.responses.ApiResponse;
import io.swagger.v3.oas.annotations.responses.ApiResponses;
import org.apache.commons.lang3.StringUtils;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.validation.Valid;
import java.util.List;

@RestController
@Api(tags = {"外部开放发票功能"})
@RequestMapping("/open")
public class OpenInvoiceController {

    @Resource
    private InvoiceService invoiceService;

    /**
     * 查询发票信息
     * @return
     */
    @ApiOperation("查看分页开票列表")
    @PostMapping("/invoice/page")
    public R<IPage<JsInvoiceOpenTaskVo>> invoicePage (@RequestBody @Valid InvoicePageReq invoicePageReq){
        return R.success(invoiceService.invoicePageVo(invoicePageReq));
    }
    /**
     * 查询发票信息
     * @return
     */
    @ApiOperation("查看开票列表（最多查询500条）")
    @PostMapping("/invoice/list")
    public R<List<JsInvoiceOpenTaskVo>> invoiceList (@RequestBody @Valid InvoiceListReq invoiceListReq){
        return R.success(invoiceService.invoiceList(invoiceListReq));
    }
    /**
     * 查询发票信息
     * @return
     */
    @ApiOperation("查询发票信息")
    @PostMapping("/invoice/get")
    public R<JsInvoiceOpenTaskVo> invoiceGet (@RequestBody @Valid InvoiceGetReq invoiceGetReq){
        return R.success(invoiceService.invoiceGet(invoiceGetReq));
    }
    /**
     * 开蓝票
     * @return
     */
    @ApiOperation("开具蓝票")
    @PostMapping("/invoice/issue")
    @ApiResponses(value = {
            @ApiResponse(responseCode = "808901001", description = "来源系统无权限"),
            @ApiResponse(responseCode = "808901002", description = "开票用户无权限"),
            @ApiResponse(responseCode = "808901003", description = "系统异常请联系工作人员"),
            @ApiResponse(responseCode = "808901004", description = "税局系统异常"),
    })
    public R<String>  invoice (@RequestBody  @Validated OpenInvoiceReq openInvoiceReq){
        try{
            String result =invoiceService.invoice(openInvoiceReq);
            if (StringUtils.isEmpty(result)){
                return R.success();
            }else {
                return R.error(result);
            }
        }finally {

        }

    }

    /**
     * 开红票
     * @return
     */
    @ApiOperation("开具红票")
    @PostMapping("/invoice/red")
    @ApiResponses(value = {
            @ApiResponse(responseCode = "808902005", description = "发票不归属于当前系没有权限红冲"),
            @ApiResponse(responseCode = "808902006", description = "发票不是正常状态无法红冲"),
            @ApiResponse(responseCode = "808902007", description = "无效的发票号码"),
            @ApiResponse(responseCode = "808901004", description = "税局系统异常"),
    })
    public R<String>  invoiceRed (@RequestBody  @Validated  OpenRedInvoiceReq openRedInvoiceReq){
        String result =invoiceService.invoiceRed(openRedInvoiceReq);
        if (StringUtils.isEmpty(result)){
            return R.success();
        }else {
            return R.error(result);
        }
    }

    /**
     * 发票回调
     * @return
     */
    @ApiOperation("发票回调")
    @PostMapping("/invoice/callback")
    public R<String>  callback (@RequestBody InvoiceCallBack invoiceCallBack){
        try{
            if (invoiceService.callBack(invoiceCallBack)){
                return R.success();
            }
            return R.error("系统报错回调失败");
        }finally {
        }

    }
    /**
     * 获取开票配置项
     * @return
     */
    @ApiOperation("获取开票配置项")
    @PostMapping("/invoice/confList")
    public R<List<InvoiceConfVo>>  confList (@RequestBody  @Validated  InvoiceConfReq invoiceConfReq){
        List<InvoiceConfVo> result =invoiceService.confList(invoiceConfReq);
        return R.success(result);
    }

}
